home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-12-20 | 4.4 KB | 177 lines | [TEXT/CWIE] |
- #include "ocheaders.h"
- #include "FnAssert.h"
- #include "Colors.h"
- #include "C3DView.h"
- #include "C3DTextView.h"
-
- //------------------------------------------------------------------------------
-
- C3DTextView::C3DTextView() : C3DView()
- {
- mText[0] = 0;
-
- mTextColor = RGB_BLACK;
-
- mTextLocation.v = 0;
- mTextLocation.h = 0;
- }
-
- //------------------------------------------------------------------------------
-
- void
- C3DTextView::Draw3D(const DrawContext * context)
- {
- inherited::Draw3D(context);
-
- Boolean isDeep = CheckScreenDepth(context) > 1;
-
- // Adjust for "button" position. If the button is depressed (poor, sad
- // little button), we move the button down and to the right by one pixel,
- // to help make it look like the button was actually pressed down
- // (unless we're only one-bit deep).
-
- Point textLocation = mTextLocation;
- if ( isDeep && IsDepressed() )
- {
- (textLocation.h)++;
- (textLocation.v)++;
- }
-
- ::MoveTo(textLocation.h, textLocation.v);
-
- ::PenState savePen;
- ::GetPenState(&savePen);
- ::PenNormal();
-
- RGBColor theForeColor;
- GetTextColor(&theForeColor);
- ::RGBForeColor(&theForeColor);
-
- SetFontParams();
-
- ::DrawString(mText);
-
- ::SetPenState(&savePen);
- }
-
- //------------------------------------------------------------------------------
-
- void
- C3DTextView::SetTextColor(const DrawContext * context, const RGBColor & textColor, Boolean redraw)
- {
- ASSERT(context != NULL, "NULL draw context!");
-
- if ( ! RGBColorsEQ(textColor, mTextColor) )
- {
- mTextColor = textColor;
-
- if ( redraw )
- Draw3D(context);
- }
- }
-
- //------------------------------------------------------------------------------
-
- void
- C3DTextView::GetTextColor(RGBColor * textColor)
- {
- *textColor = mTextColor;
- }
-
- //------------------------------------------------------------------------------
-
- Boolean
- C3DTextView::PlaceText(const DrawContext * context, const StringPtr text)
- {
- ASSERT(context != NULL, "NULL draw context!");
- ASSERT(text != NULL, "NULL text!");
-
- Boolean textFits = true;
-
- // Calculate the text location (mTextLocation). This is the "home"
- // location, and may actually be modified in the Draw method, based
- // on IsDepressed().
-
- // Calculate the horizontal and vertical sizes.
-
- SetFontParams();
- short textWidth = ::StringWidth(text);
-
- FontInfo fontInfo;
- ::GetFontInfo(&fontInfo);
- short textHeight = fontInfo.ascent + fontInfo.descent;
-
- // Now compute mTextLocation to center the text in the view, with some
- // possible minor adjustments based on fState, to make the "button"
- // look like it's being pushed down and let up.
-
- Rect extent = context->Location;
-
- // horizontally...
- long leftOverH = (extent.right - extent.left) - textWidth;
- textFits = textFits && leftOverH > 0;
- leftOverH = leftOverH < 0 ? 0 : leftOverH; // limit, in case text is too wide
- mTextLocation.h = (short)(extent.left + (leftOverH / 2));
-
- // ... and vertically...
- long leftOverV = (extent.bottom - extent.top) - textHeight;
- textFits = textFits && leftOverV > 0;
- leftOverV = leftOverV < 0 ? 0 : leftOverV; // limit, in case text is too tall
- mTextLocation.v = (short)
- (extent.bottom - fontInfo.descent - (leftOverV / 2) - 1);
- // Note that the -1 on the end is because
- // normally the text looks a little
- // too low when it's "properly" centered,
- // so we move it up a pixel.
-
- return textFits;
- }
-
- //------------------------------------------------------------------------------
-
- void
- C3DTextView::SetText ( const DrawContext * context, const char * text, Boolean redraw )
- {
- ASSERT(context != NULL, "NULL draw context!");
- ASSERT(text != NULL, "NULL text!");
-
- Str255 inputText;
- if ( strlen(text) > 255 ) // MMF: use a constant here!
- strncpy((char *)inputText, text, 255); // MMF: use a constant here!
- else
- strcpy((char *)inputText, text);
-
- c2pstr((char *)inputText);
-
- if ( PLstrcmp(inputText, mText) ) // if it's NOT the same text
- {
- PLstrcpy(mText, inputText);
-
- Boolean textFits = PlaceText(context, mText);
-
- if ( redraw && textFits )
- Draw3D(context);
- }
- }
-
- //------------------------------------------------------------------------------
-
- void
- C3DTextView::GetText ( char * text )
- {
- ASSERT(text != NULL, "NULL input pointer!");
-
- PLstrcpy((StringPtr)text, mText);
- p2cstr((StringPtr)text);
- }
-
- //------------------------------------------------------------------------------
- void
- C3DTextView::SetFontParams(void)
- {
- ::TextFace(bold);
- ::TextFont(applFont);
- ::TextSize(12);
- ::TextMode(srcCopy);
- }
-